package com.xhui.vimarket.zkplus.zul.attr.msg;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Timebox;

import com.xhui.crm.common.PageConstants;
import com.xhui.crm.common.exception.RtManagerException;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.util.ZkWindUtil;

/**
 * 规则执行类.
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-3-4
 * @功能说明：
 */
public final class AttrMsgSupport {
    
    /**
     * 私有构造函数.
     * 
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    private AttrMsgSupport() {
    }
    
    /**
     * validate.
     * 
     * @param retVo RetVO
     * @param submitListener EventListener
     * @param cancelListener EventListener
     * @return boolean
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    public static boolean validate(final RetVO retVo, final EventListener submitListener,
        final EventListener cancelListener) {
        final List<RetVO> retVoList = new ArrayList<RetVO>();
        retVoList.add(retVo);
        return AttrMsgSupport.validateBatch(retVoList, submitListener, cancelListener);
    }
    
    /**
     * validate.
     * 
     * @param retVoList List<RetVO>
     * @param submitListener EventListener
     * @param cancelListener EventListener
     * @return boolean
     * @author wuyx
     * 2011-3-22 wuyx 
     */
    @SuppressWarnings("unused")
    public static boolean validateBatch(final List<RetVO> retVoList,
        final EventListener submitListener, final EventListener cancelListener) {
        boolean retIsTrue = false;
        try {
            int msgVoSum = 0;
            if (retVoList != null) {
                for (RetVO retVo : retVoList) {
                    if (retVo != null && retVo.getDataList() != null) {
                        for (Object oSubRetVo : retVo.getDataList()) {
                            msgVoSum++;
                        }
                    }
                }
            }
            if (msgVoSum < 1) {
                // 直接触发
                if (submitListener != null) {
                    submitListener.onEvent(null);
                }
                retIsTrue = true;
            } else {
                // 展示界面
                final AttrMsgExt ruleInfoExt = (AttrMsgExt) Executions.createComponents(
                    PageConstants.ATTR_MSG, null, null);
                ruleInfoExt.setRetVoList(retVoList);
                ruleInfoExt.setSubmitListener(submitListener);
                ruleInfoExt.setCancelListener(cancelListener);
                ruleInfoExt.display();
                ZkWindUtil.doModal(ruleInfoExt, ZkWindUtil.WIN_MIN_SIZE);
                ruleInfoExt.setSizable(true);
                ruleInfoExt.setClosable(false);
            }
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new RtManagerException(ex);
        }
        return retIsTrue;
    }
    
    /**
     * .
     * 
     * @param valiRetVo List<RetVO>
     * @param attrComp Component
     * @author wuyx
     * 2011-6-1 wuyx 
     */
    public static void validateThrow(List<RetVO> valiRetVo, Component attrComp) {
        if (valiRetVo != null && valiRetVo.size() > 0) {
            StringBuffer sbuf = new StringBuffer();
            for (RetVO ret : valiRetVo) {
                sbuf.append(ret.getRetMsg() + "\n");
            }
            throw new WrongValueException(getInputComp(attrComp), sbuf.toString());
        }
    }
    
    /**
     * 获取下级输入型控件.
     * 
     * @param comp Component
     * @return Component 
     * @author wuyx
     * 2011-6-3 wuyx 
     * 1.textbox,intbox,checkbox,datebox,timebox
     */
    public static Component getInputComp(Component comp) {
        Component ret = getInputComp(comp, 0, 3);
        return ret == null ? comp
            : ret;
    }
    
    /**
     * 获取下级输入型控件.
     * 
     * @param comp Component
     * @param curdeep int
     * @param maxdeep int
     * @return Component 
     * @author wuyx
     * 2011-6-3 wuyx 
     * 1.textbox,intbox,checkbox,datebox,timebox
     */
    private static Component getInputComp(Component comp, int curdeep, int maxdeep) {
        if (comp == null || curdeep > maxdeep) {
            return null;
        }
        if (isInputComp(comp)) {
            return comp;
        }
        if (comp.getChildren() != null) {
            for (Object subCompObject : comp.getChildren()) {
                if (subCompObject instanceof Component) {
                    Component subComp = (Component) subCompObject;
                    Component subRet = getInputComp(subComp, curdeep + 1, maxdeep);
                    if (subRet != null) {
                        return subRet;
                    }
                }
            }
        }
        return null;
    }
    
    /**
     * 是否输入类控件.
     *  
     * @param comp Component
     * @return boolean
     * @author wuyx
     * 2011-6-3 wuyx 
     */
    private static boolean isInputComp(Component comp) {
        if (comp == null) {
            return false;
        }
        if (comp instanceof Textbox || comp instanceof Intbox || comp instanceof Checkbox
            || comp instanceof Datebox || comp instanceof Timebox) {
            return true;
        }
        return false;
    }
}
